package com.centaline.androidsalesblog.rx.request;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.api.usercenter.SmsApi;
import com.centaline.androidsalesblog.model.responseobject.SmsRo;
import com.centanet.framework.interfaces.IResponseListener;

import rx.Observable;
import rx.Subscriber;

/**
 * 获取手机验证码
 * Created by guilin on 16/3/18.
 */
final class RequestSmsOnSubscribe implements Observable.OnSubscribe<SmsRo> {

    private final Class<?> tag;
    private final String mobile;

    public RequestSmsOnSubscribe(Class<?> tag, String mobile) {
        this.tag = tag;
        this.mobile = mobile;
    }

    @Override
    public void call(final Subscriber<? super SmsRo> subscriber) {
        SmsApi smsApi = new SmsApi(tag, SmsRo.class, new IResponseListener<SmsRo>() {
            @Override
            public void onResponse(SmsRo smsRo) {
                if (!subscriber.isUnsubscribed()) {
                    subscriber.onNext(smsRo);
                }
                subscriber.onCompleted();
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                subscriber.onError(error);
            }
        });
        smsApi.setMobile(mobile);
        smsApi.startRequest();
    }
}
